package net.janeluo.rs.service;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import net.janeluo.rs.entity.RS_Orders;

import org.apache.commons.lang.StringUtils;
import org.nutz.dao.Cnd;
import org.nutz.dao.Dao;
import org.nutz.dao.sql.Criteria;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.log.Log;
import org.nutz.log.Logs;
import org.nutz.service.Service;

/**
 * 订单管理业务
 * 
 * @author DLK)JaneLuo
 * @version 1.0.0
 */
@IocBean(fields = { "dao" })
public class OrdersCalendarService extends Service {
    private static final Log log = Logs.getLog(OrdersCalendarService.class);

    public OrdersCalendarService() {
        super();
    }

    public OrdersCalendarService(Dao dao) {
        super(dao);
    }

    public List<List<Map<String, Object>>> initCalendar(Integer year, Integer month) {

        // 订单数量
        String orderKey = "ordercount";
        // 订餐人数
        String userKey = "usercount";

        Criteria cri = Cnd.cri();
        if (year != null && month != null) {
            cri.where().andLike("orderTime", StringUtils.leftPad(year + "", 2, "0") + "/" + StringUtils.leftPad(month + "", 2, "0"));
        } else {
            Date date = new Date();
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/MM");// 可以方便地修改日期格
            String dateStr = dateFormat.format(date);
            cri.where().andLike("orderTime", dateStr);
        }

        Map<String, Map<String, Integer>> order4dayMap = new HashMap<String, Map<String, Integer>>();
        Map<String, String> _userofday = new HashMap<String, String>();

        // 查询所选月订单信息
        List<RS_Orders> orderslst = dao().query(RS_Orders.class, cri);
        for (RS_Orders o : orderslst) {
            String day = o.getOrderTime().substring(0, 10); // 当前天
            // 当前天订单Map
            Map<String, Integer> dayOrderMap = order4dayMap.get(day);
            if (dayOrderMap == null) {
                dayOrderMap = new HashMap<String, Integer>();
            }

            // 计算订单数量
            Integer orderconunts = dayOrderMap.get(orderKey);
            if (orderconunts == null) {
                orderconunts = 0;
            }
            orderconunts += Integer.parseInt(o.getQuantity());
            dayOrderMap.put(orderKey, orderconunts);
            // 计算订餐人数
            String userid = o.getUserid();
            String user = _userofday.get(day + userid);
            if (user == null) {
                _userofday.put(day + userid, userid);
                Integer userconunts = dayOrderMap.get(userKey);
                if (userconunts == null) {
                    userconunts = 0;
                }
                userconunts++;
                dayOrderMap.put(userKey, userconunts);
            }

            order4dayMap.put(day, dayOrderMap);
        }

        List<List<Map<String, Object>>> rowlst = new LinkedList<List<Map<String, Object>>>();

        // 当前月份的1号是星期中的第几天
        int dayOfWeek = getDayOfWeek(year, month);
        // 当前月一共有多少天
        int dayofmonth = getDays4Month(year, month);

        int count = 1; // 总个数
        int day = 0; // 天数
        boolean showday = false;
        for (int i = 1; i <= 6; i++) {
            List<Map<String, Object>> celllst = new LinkedList<Map<String, Object>>();
            for (int j = 0; j < 7; j++) {
                Map<String, Object> dayMap = new HashMap<String, Object>();
                if (count == dayOfWeek) {
                    showday = true;
                }
                if (showday && day != dayofmonth) {
                    day++;
                    String _day = StringUtils.leftPad(year + "", 2, "0") + "/" + StringUtils.leftPad(month + "", 2, "0") + "/"
                            + StringUtils.leftPad(day + "", 2, "0");

                    Map<String, Integer> map = order4dayMap.get(_day);
                    if (map != null) {
                        dayMap.putAll(map);
                    }
                    dayMap.put("day", day);
                }
                count++;
                celllst.add(dayMap);
            }
            rowlst.add(celllst);
        }

        // System.out.println("当月天数:"+cal.get(Calendar.DAY_OF_MONTH));
        return rowlst;
    }

    /**
     * 取得一个月有多少天
     * 
     * @param year
     * @param month
     * @return
     */
    private int getDays4Month(Integer year, Integer month) {
        Calendar cal = Calendar.getInstance();
        year = year == null ? cal.get(Calendar.YEAR) : year;
        month = month == null ? cal.get(Calendar.MONTH) +1: month ;
        cal.set(Calendar.YEAR, year);
        cal.set(Calendar.MONTH, month);
        cal.set(Calendar.DAY_OF_MONTH, 1);
        cal.set(Calendar.DATE, cal.get(Calendar.DATE) - 1);
        return cal.get(Calendar.DAY_OF_MONTH);
    }

    private int getDayOfWeek(Integer year, Integer month) {
        GregorianCalendar calendar = new GregorianCalendar(); 
        year = year == null ? calendar.get(Calendar.YEAR) : year;
        month = month == null ? calendar.get(Calendar.MONTH)  : month - 1;
        calendar.set(Calendar.YEAR, year);
        calendar.set(Calendar.MONTH, month);
        // 设置now的日期为1  
        calendar.set(Calendar.DAY_OF_MONTH, 1); 
        // 得到now是一周的第几天  
        int week = calendar.get(Calendar.DAY_OF_WEEK); 
        
        return week;
    }

    public static void main(String[] args) {
        OrdersCalendarService a = new OrdersCalendarService();
        a.initCalendar(null, null);
    }
}
